package cn.group01.git.controller;


import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 验证码
 */
@Controller
@RequestMapping("captcha")
public class LineCaptchaController {

    /**
     * 获取验证码
     * 1.将验证码以流的形式写出（获取流 ==> HttpServletResponse ==> 导入javax.servlet-api依赖）
     * 2.将验证码放入session里 ==> 直接注入session
     */
    @RequestMapping("getCode")
    public void getCode(HttpServletResponse response, HttpSession session){
        // 设置宽高
        LineCaptcha lineCaptcha = new LineCaptcha(110, 50, 4, 2);
        // 获取验证码
        String code = lineCaptcha.getCode();
        // 将验证码存入session 用于登录认证
        session.setAttribute("code",code);

        try {
            // 通过响应对象 获取输出流 将验证码图片写出到浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
